home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / c_toolbx.arc / INSERTI.C < prev    next >
Encoding:
Text File  |  1988-03-30  |  594 b   |  25 lines

  1. /*  inserti.c - insertion sort for an array of integers  */
  2. #include "stdio.h"
  3.  
  4. int  insert(a,na)
  5.   int    a[]   ;         /* array of integers to be sorted    */
  6.   int    na    ;         /* number of integers to be sorted    */
  7.   {
  8.      int   i  ,  j   ;        /* indices for loops    */
  9.      int   temp  ;        /* holds one element of array temporarily */
  10.  
  11.      for( i=1 ; i < na ; i = i + 1 )
  12.     {  /* insert the i-th element into the array */
  13.        temp = a[i]    ;
  14.        j = i - 1  ;
  15.        while( ( j >= 0 ) && ( temp < a[j] ) )
  16.           {  a[j+1] = a[j]    ;
  17.          j = j - 1  ;
  18.           }
  19.        a[j+1] = temp  ;
  20.     }
  21.   }
  22.  
  23.  
  24.  
  25.